package com.wanho.java156.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanho.java156.mapper.MemberMapper;
import com.wanho.java156.mapper.OrderMapper;
import com.wanho.java156.mapper.ProductMapper;
import com.wanho.java156.mapper.TravellerMapper;
import com.wanho.java156.po.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiresRoles("订单管理员")
public class OrderService {
    @Autowired
    private OrderMapper orderMapper ;
    @Autowired
    private ProductMapper productMapper ;
    @Autowired
    private MemberMapper memberMapper ;
    @Autowired
    private TravellerMapper travellerMapper ;

   /* public Order view(String id){
        //1. 根据id 查询 单表
        Order order = orderMapper.selectByPrimaryKey(id);
        //查 产品
        String productid = order.getProductid();
        Product product = productMapper.selectByPrimaryKey(productid);
        order.setProduct(product);
        //2.查询 会员id  会员对象 order：member
        String memberid = order.getMemberid() ;
        Member member = memberMapper.selectByPrimaryKey(memberid);
        order.setMember(member);
        //3. 根据订单id  查询 关联所有游客   travellers赋值
        List<Traveller> travellers = travellerMapper.selectTravellersByOrderId(id);
        order.setTravellers(travellers);
        return  order ;
    }*/
    /**
     * 根据订单编号 查询该订单关联的商品，会员，旅客等信息
     * @param id
     * @return
     */
    public Order view(String id){
        return orderMapper.selectCascadeById(id) ;
    }

    /**
     * 分页 + 条件查询
     * @param pageNum 页码 第几页？
     * @param pageSize 分页跨度  每页显示多少行？
     * @param ordernum  模糊查询的用户条件
     * @return
     */
    public PageInfo<Order> list(int pageNum, int pageSize, String ordernum){
        //获取第1页，10条内容，默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        OrderExample orderExample = new OrderExample();
        //判断是否带模糊条件查询
        if (StringUtils.isNotEmpty(ordernum)){
            orderExample.createCriteria().andOrdernumLike("%"+ordernum+"%") ;
        }
        List<Order> orderList = orderMapper.selectByExample(orderExample);
        //设置关联表数据
        orderList.forEach(o->{
            Product product = productMapper.selectByPrimaryKey(o.getProductid());
            o.setProduct(product);
        });
        return new PageInfo<>(orderList) ;
    }
   /* 1. 单表 查询全部
   public List<Order> list(){
        OrderExample orderExample = new OrderExample();
        List<Order> orderList = orderMapper.selectByExample(orderExample);
        //设置关联表数据
        orderList.forEach(o->{
            Product product = productMapper.selectByPrimaryKey(o.getProductid());
            o.setProduct(product);
        });
        return orderList ;
    }*/


}
